<?php
// 应用公共文件

use think\facade\Db;

function auto_backup(){
    $root_path = app() -> getRootPath();
    $db_path = $root_path.'db_backup';
    if(!file_exists($db_path)){//如果目录不存在
        mkdir($db_path);
    }
    if(!is_dir($db_path)){//是目录才能继续，否则跳过
        return;
    }
    $target_file = $db_path.'/'.date('Y-m-d');
    $all_db_name = ['goods','category','daily','depart','local','suppliers','user'];
    foreach($all_db_name as $db_name){
        if(!file_exists($target_file.'_'.$db_name.'.json')){//如果不存在，则创建
            $db = json_encode(Db::name($db_name) -> select() -> toArray());
            $f = fopen($target_file.'_'.$db_name.'.json','w');
            fwrite($f,$db);
            fclose($f);
        }
    }
}

function rsa_get_private_key(){
    return '-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQD0hEB+6RXO7UDX
es6ptLCAwPmOtqDFp6FiiLXX0mEJWrgtBacFxtU4fAM88Tm5g4dthN3doEYnzxBo
5oE845dBmn53eprbIeTPHkCC6PznC3VkvEMsVWaQDYG5ClwivyUy22+KRzW8vlet
dKK7XMjDoxH9Mcw2a5VSDtfOtqanYY4g344HL+QxWmzMfFHuxm2S+lLjDsiS9QMp
uPCyFTYhl+NX1LaJSBDaElHEELFcZRpPkVIm/vl7T5q0c3AoXPYA/7N75EPkL6HR
70HymtxCkSm7Aus8KCpGA2q2Oh29JVSP8jfEncUWTsLWmld2+B3qIukw9jmGrWH1
lgi4GU0HAgMBAAECggEBALaKnROhsvQ+TvAO8IXlLPeSGTyfvrqQgHztDsRGfMbd
AS4rUBuR6tA2VLyEYdHLDamHTZMM+hLyFJAbtVPdnKgPTGaqEz2Quh6duQ+CHHvo
JAXGPme0gdL9ImEgCNSv2ndzNnwG2WlEuHP35Rz3EbJr5RwetxPRkTInfpkF5riB
OF3dLjd8AGOnlcUUW8/UEFdTJSLMC5WT2cDKL2SodG7drj1cPPUiJ0PjjhoXI0Dl
AeEXCPWi7hNKZ+tgh2UPWp8A7TKc77+NyZhxaDaiiLQXRXSCz9+FAX7zKa+IHSx9
FxUcEZNJx7gWHr3eyxctBHjs4FTQtWDbtRFvmJKw4wECgYEA/dPtJPR8GfTT7lrf
Xlm5MkOe0vmDU+F/m7SqKL34BioBVHYBbZGc261SXgi6p2FzD2aI00n35Oqf6z66
vj3C7L2goGHOseGFR0l6cKDj1cLLfvoX3gnDpdN5OIjajY//N/vcTOwzMBDA4S+/
fXDp2Fv0p8cVz6HQx9cnBj7le8ECgYEA9pvtUEynttuBqYYaOzEXvMiS3c141kTQ
8W6cm/ySArCIGbky06vFuTAVcwxWZfHRnmEnVcPBewZUL3oH1gsB2Tesk2RfgI12
WR58E6e99N+GaOZcGcC6420QNGcREtjSgBDsqV7fCheIl6fEi9ILktk8zjKrYIpu
Cr7QPOhEmscCgYEArFbE789zp2f8sRxQBs6imIcjAhnZWg2ER+LtmYYKLZ09k+wf
YhZ4ywquHxyMJvNaXP2ALv1M68eF1wKbZmmmvHiv49Ukss/tXw57rJcAunVtF4SU
yfQVO7UO3BzHT1CSt58w36WLx7nIS9zQhkow6+7beUMKAxTaHaVuYKY5x8ECgYEA
zHIgn/2s3Q/FwwEGJiRhBYVTHMcHcrvCbSXCeB8//TLU4l6DILC7TvfqEGMeDY4Y
tjDdJ2EOuitZB7MnY+/kLGzXiaog8rZUczWur6zU8nmNONIhcMgzJ/B3CrqXkQRo
Hvvo99FLH1a7St8vNquAJNIsctXW9kZNTGM8yAHXPmkCgYANlLECLc6MYAHTPz3P
fwHlG3GTpdhUx/EPmiR6VLYUT0RhwXebHorxuZaiLv26pzLc7JVvphi4BhitP78i
kC2EhGhGGbv9RH8jHFZ7mKf9SBMVyhwOH+yn+vdZo5j++oZFT1gTqpOCkE+I229s
S5kmdvGoGqgngr9OJTpzxCU5XA==
-----END PRIVATE KEY-----';
}

function rsa_get_public_key(){
    return '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9IRAfukVzu1A13rOqbSw
gMD5jragxaehYoi119JhCVq4LQWnBcbVOHwDPPE5uYOHbYTd3aBGJ88QaOaBPOOX
QZp+d3qa2yHkzx5Aguj85wt1ZLxDLFVmkA2BuQpcIr8lMttvikc1vL5XrXSiu1zI
w6MR/THMNmuVUg7Xzramp2GOIN+OBy/kMVpszHxR7sZtkvpS4w7IkvUDKbjwshU2
IZfjV9S2iUgQ2hJRxBCxXGUaT5FSJv75e0+atHNwKFz2AP+ze+RD5C+h0e9B8prc
QpEpuwLrPCgqRgNqtjodvSVUj/I3xJ3FFk7C1ppXdvgd6iLpMPY5hq1h9ZYIuBlN
BwIDAQAB
-----END PUBLIC KEY-----';
}

function rsa_private_decrypt($enc_text){
    mb_internal_encoding("UTF-8");
    if(gettype($enc_text) !== 'string'){
        return null;
    }
    //halt(2);
    $enc_text_base64_decode = base64_decode($enc_text);
    //exit;
    return (openssl_private_decrypt($enc_text_base64_decode, $decrypted, rsa_get_private_key())) ? $decrypted : null;
}

function generate_password_with_salt($password){
    $salt_start = '8djn4289jfn$%%^nv89asj';
    $salt_end = '-=aop1.ed[fp/~$%jd';
    $password = strval($password);
    return md5($salt_start.$password.$salt_end);
}